import 'package:calc/calc_vm.dart';
import 'package:decimal/decimal.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

//计算器
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Calc',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: '计算器'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  CalcViewModel model = CalcViewModel();

  @override
  Widget build(BuildContext context) {
    Decimal? valueK = (Decimal.tryParse(model.b.toString())! -
            Decimal.tryParse(model.c.toString())!) *
        Decimal.fromInt(100000);
    Decimal? valueM = (Decimal.tryParse(model.a.toString())! -
            Decimal.tryParse(model.c.toString())!) *
        Decimal.fromInt(100000);
    Decimal? valueY = Decimal.tryParse(model.c.toString())! -
        Decimal.tryParse(0.00018.toString())!;
    Decimal? valueX = (Decimal.tryParse(model.a.toString())! +
            valueK / Decimal.fromInt(80000)) -
        Decimal.tryParse(0.00018.toString())!;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              children: [
                Expanded(child: _resWidget("K", valueK)),
                Expanded(child: _resWidget("M", valueM)),
              ],
            ),
            Padding(
              padding: EdgeInsets.all(20),
              child: Row(
                children: [
                  Expanded(child: _resWidget("X", valueX)),
                  Expanded(child: _resWidget("Y", valueY)),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.all(20),
              child: Row(
                children: [
                  Expanded(
                    child: _inputWidget(
                      "A",
                      (value) {
                        double? res = double.tryParse(value);
                        if (res != null) {
                          model.a = res;
                          setState(() {});
                        }
                      },
                    ),
                  ),
                  Expanded(
                    child: _inputWidget(
                      "B",
                      (value) {
                        double? res = double.tryParse(value);
                        if (res != null) {
                          model.b = res;
                          setState(() {});
                        }
                      },
                    ),
                  ),
                  Expanded(
                    child: _inputWidget(
                      "C",
                      (value) {
                        double? res = double.tryParse(value);
                        if (res != null) {
                          model.c = res;
                          setState(() {});
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      // floatingActionButton: FloatingActionButton(
      //   onPressed: _incrementCounter,
      //   tooltip: 'Increment',
      //   child: Icon(Icons.add),
      // ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  ///结果组件
  Widget _resWidget(String title, Decimal value) {
    return Column(
      children: [
        Text("$title"),
        GestureDetector(
          onTap: () async {
            await Clipboard.setData(ClipboardData(text: value.toString()));
            Fluttertoast.showToast(
                msg: "已复制到剪切板",
                toastLength: Toast.LENGTH_SHORT,
                gravity: ToastGravity.CENTER,
                timeInSecForIosWeb: 1,
                backgroundColor: Colors.black,
                textColor: Colors.white,
                fontSize: 16.0);
          },
          child: Container(
            constraints: BoxConstraints(
              minWidth: 60,
              minHeight: 30,
            ),
            padding: EdgeInsets.symmetric(
              horizontal: 10,
            ),
            decoration: BoxDecoration(
              color: Colors.transparent,
              border: Border.all(
                color: Colors.black,
              ),
            ),
            child: Text(
              "$value",
              textAlign: TextAlign.center,
              style: TextStyle(
                height: 2,
              ),
            ),
          ),
        ),
      ],
    );
  }

  ///输入项
  Widget _inputWidget(String title, ValueChanged<String>? onChange) {
    return Column(
      children: [
        Text("$title"),
        Container(
          constraints: BoxConstraints(
            minWidth: 60,
            minHeight: 30,
          ),
          decoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(
              color: Colors.black,
            ),
          ),
          child: CupertinoTextField(
            // controller: TextField.con,
            keyboardType: TextInputType.number,
            onChanged: onChange,
          ),
        ),
      ],
    );
  }
}
